package action;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import model.Task;
import model.User;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.github.miemiedev.mybatis.paginator.domain.PageList;

import service.TaskService;
import action.base.BaseAction;

@Controller
@Scope("prototype")
public class TaskAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private TaskService taskService;
	private Task task;
	private boolean success = false;
	private PageList<HashMap<String, Object>> pageList;
	HashMap<String, Object> taskInfo;
	List<HashMap<String, Object>> receivers;
	private Integer total;
	private Date currentTime;

	public String saveOrUpdate() {
		String receids = super.getParameterStr("receids");
		User user = super.getUser();
		task.setSenderid(user.getId());
		task.setSendtime(new Date());
		taskService.saveOrUpdate(task, receids);
		success = true;
		return "success";
	}

	public String queryTime() {
		currentTime = new Date();
		return "currentTime";
	}

	public String queryPage() throws UnsupportedEncodingException {
		int page = super.getParameterInt("page");
		int limit = super.getParameterInt("limit");
		String title = super.getParameterStr("query");
		String part = super.getParameterStr("part");
		Integer status = super.getParameterInt("status");
		User user = super.getUser();
		if (!StringUtils.isNotBlank(title)) {
			title = null;
		} else {
			byte[] _title = title.getBytes("ISO-8859-1");
			title = new String(_title, "UTF-8");
		}
		if (status != null) {
			if (status == 0) {
				status = null;
			}
		}
		HashMap<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("title", title);
		queryMap.put("temp", user.getId());
		queryMap.put("part", part);
		queryMap.put("status_query", status);
		pageList = taskService.queryPage(page, limit, queryMap);
		setTotal(pageList.getPaginator().getTotalCount());
		return "queryPage";
	}

	public String read() {
		Integer id = super.getParameterInt("id");
		String role = super.getParameterStr("role");
		taskService.read(id, role);
		setSuccess(true);
		return "success";
	}

	public String delete() {
		String ids = super.getParameterStr("ids");
		taskService.delete(ids);
		success = true;
		return "success";
	}

	public String submit() {
		String ids = super.getParameterStr("ids");
		taskService.submit(ids);
		success = true;
		return "success";
	}

	public String getById() {
		Integer id = super.getParameterInt("taskId");
		taskInfo = taskService.getById(id);
		success = true;
		return "taskInfo";
	}

	public String getByTask() {
		Integer taskid = super.getParameterInt("taskId");
		receivers = taskService.getByTask(taskid);
		success = true;
		return "receivers";
	}

	public String feedback() {
		Integer taskid = super.getParameterInt("taskId");
		String finishtime = super.getParameterStr("finishtime");
		String finishcontent = super.getParameterStr("finishcontent");
		User user = super.getUser();
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("id", taskid);
		map.put("finishtime", finishtime);
		map.put("finishcontent", finishcontent);
		map.put("status", 3);
		map.put("feedbacktime", new Date());
		map.put("managerid", user.getId());
		map.put("isread", 2);
		taskService.feedback(map);
		success = true;
		return "success";
	}

	public Task getTask() {
		return task;
	}

	public void setTask(Task task) {
		this.task = task;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public PageList<HashMap<String, Object>> getPageList() {
		return pageList;
	}

	public void setPageList(PageList<HashMap<String, Object>> pageList) {
		this.pageList = pageList;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	public HashMap<String, Object> getTaskInfo() {
		return taskInfo;
	}

	public void setTaskInfo(HashMap<String, Object> taskInfo) {
		this.taskInfo = taskInfo;
	}

	public List<HashMap<String, Object>> getReceivers() {
		return receivers;
	}

	public void setReceivers(List<HashMap<String, Object>> receivers) {
		this.receivers = receivers;
	}

	public Date getCurrentTime() {
		return currentTime;
	}

	public void setCurrentTime(Date currentTime) {
		this.currentTime = currentTime;
	}

}
